package com.onlygx.dva.web.base.service;

import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.onlygx.dva.common.properties.CommonAliOSSProperties;
import com.onlygx.dva.common.properties.CommonCoreProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.InputStream;

/**
 * <p>
 *  Ali oss 服务
 * </p>
 *
 * @author 高祥
 * @since 2023-07-28
 */
@Slf4j
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(value = "dva-core.use-oss", havingValue = "ali")
public class AliOSSService implements OSSService{

    final CommonCoreProperties commonCoreProperties;

    final CommonAliOSSProperties commonAliOSSProperties;

    private OSS ossClient;


    @PostConstruct
    public void init() {
        try {
            log.info("初始化Aliyun OSS服务");
            ossClient = new OSSClientBuilder().build(commonAliOSSProperties.getEndpoint()
                    , commonAliOSSProperties.getAccessKey()
                    , commonAliOSSProperties.getSecretKey());

        } catch (Exception e) {
            log.error("AliOSS初始化失败，原因：{}",e.toString());
            //e.printStackTrace();
            //throw new RuntimeException(e);
        }
    }

    @Override
    public String pushFile(File file) {
        return pushFile(file, null);
    }

    @Override
    public String pushFile(String name, InputStream inputStream) {
        return pushFile(name, null, inputStream);
    }


    @Override
    public String pushFile(File file, String dir) {
        String fileName = commonAliOSSProperties.getBaseDir() + "/";
        if (StrUtil.isEmpty(dir)){
            fileName += file.getName();
        }else{
            fileName += dir + "/" + file.getName();
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest(
                commonAliOSSProperties.getBucketName(), fileName, file);
        ossClient.putObject(putObjectRequest);
        return "https://"
                + commonAliOSSProperties.getBucketName()
                + "."
                + commonAliOSSProperties.getEndpoint()
                + "/"+ fileName;
    }

    @Override
    public String pushFile(String name, String dir, InputStream inputStream) {
        String fileName = commonAliOSSProperties.getBaseDir() + "/";
        if (StrUtil.isEmpty(dir)){
            fileName += name;
        }else{
            fileName += dir + "/" + name;
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest(
                commonAliOSSProperties.getBucketName(), fileName, inputStream);
        ossClient.putObject(putObjectRequest);
        return "https://"
                + commonAliOSSProperties.getBucketName()
                + "."
                + commonAliOSSProperties.getEndpoint()
                + "/"+ fileName;
    }

}
